package com.test.service.impl;

import com.test.mapper.PermissionMapper;
import com.test.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.test.po.Permission;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Lenovo on 2019/1/9.
 */
@Service
public class PermissionServiceImpl implements PermissionService{

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> getPermissionByRoleId(int roleId) {
        return permissionMapper.getPermissionByRoleId(roleId);
    }

    @Override
    public Map<String, List<Permission>> getPermissionMapByUserId(Integer userId) {
        List<Permission> permissionList = permissionMapper.getPermissionList(userId);
        List<Permission> menuList = new ArrayList<>();
        Map<Integer, Permission> map = new HashMap<>();
        //筛选目录
        for(int i = 0, length=permissionList.size(); i<length; i++){
            Permission permission = permissionList.get(i);
            if(permission.getType() != 3 && permission.getPid() == 0){
                map.put(permission.getId(), permission);
                menuList.add(permission);
            }
        }
        //封装菜单
        for(int i=0, length=permissionList.size(); i<length; i++){
            Permission permission = permissionList.get(i);
            if(permission.getType() != 3 && map.get(permission.getPid()) != null){
                Permission parent = map.get(permission.getPid());
                parent.getChildren().add(permission);
            }
        }
        Map<String, List<Permission>> resultMap = new HashMap<>(2);
        resultMap.put("menuList", menuList);
        resultMap.put("permissionList", permissionList);
        return resultMap;
    }
}
